<?php  
	namespace Home\Controller;
	use Think\Controller;
	//奖惩信息管理
	//@author  程传民
	class PunishController extends AllowController{

		public function index(){
			$performance = D("performance");

			$tot = $performance->count();

			//分页管理
			$nums = 5;
	    	$page = new \Think\Page($tot,$nums);

			$users = D("users");
			$class = D("class");

			$info = $performance->limit($page->firstRow,$page->listRows)->order('created asc')->select();

			$list = array();
			foreach($info as $key=>$value){
				$stu = array("id"=>$value['stu_id']);	//学员users的id
				$t = array("id"=>$value['do_id']);		//教师users中的id
				$stu_class = array("id"=>$value['class']);

				$stu_class = $class->where($stu_class)->field("name")->find();
				$list[$key]['class'] = rtrim($stu_class['name'],"班")."班";
				$stu_name = $users->where($stu)->field("name")->find();	//学员姓名
				$list[$key]['stu_name'] = $stu_name['name'];
				$t_name = $users->where($t)->field('name')->find();	//教师姓名
				$list[$key]['t_name'] = $t_name['name'];
				// $list[$key]['stu_name'] = $users->where($stu)->field("name")->find();
				// $list[$key]['t_name'] = $users->where($t)->field('name')->find();
				$value['created'] = date("Y-m-d",$value['created']);
				$list[$key] += $value;
				// var_dump($list[$key]['stu_name']);
				// var_dump($list[$key]['t_name']);
			}

			// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);
			$this->assign("list",$list);
			$this->display("Punish/punish");
		}

		public function add(){
			$class = D("class");

			$users = D("users");

			$list = $users->select();
			$classs = $class->where("status=1")->select();			

			$this->assign("classs",$classs);
			$this->assign("list",$list);
			$this->display("Punish/add");
		}

		public function select(){
			$arr["class"]=$_POST['classid'];
			// $arr["class"]=5;
			$users = D("users");

			$info = $users->where($arr)->where("can_login=2")->select();
			// var_dump($info);
			// exit;
			echo json_encode($info);
		}

		public function insert(){

			$arr['id'] = I("post.name");

			$users = D("users");

			$list1 = $users->where(array("id"=>I("post.name")))->find($arr);
			// exit;
			//新的绩效分
			$arrs['performance'] = $list1['performance']-I("post.score");
			$a = $users->where($arr)->save($arrs);

			//要把这些数据存储到绩效分表中
			$performance = D("performance");

			$data['stu_id'] = I("post.name");			//被处罚人的id
			$data['class'] = I("post.classs");			//所在班级id
			$data['do_id'] = $_SESSION['userid'];		//执行人的id
			$data['score'] = I("post.score");			//扣分数
			$data['cause'] = I("post.cause");			//扣分原因
			$data['created'] = strtotime(I("post.addtime"));	//执行时间
			$data['status'] = 1;						//状态	1生效 0撤销
			
			$info = $performance->add($data);

			if($info){
				echo "添加成功";
			}else{
				echo "添加失败";
			}

		}

		//撤销惩罚
		public function back(){
			$arr['id'] = I("post.id");
			
			$performance = D("performance");
			$data['status'] = 0;
			$info = $performance->where($arr)->save($data);

			$list = $performance->where($arr)->find();
			$score = $list['score'];
			// var_dump($score);  拿到分值了
			// exit;

			$users = D("users");

			$arr1['id'] = I("post.stuid");
			
			$oldper = $users->where($arr1)->find();	//原来老的分值
			$oldperformance = $oldper['performance'];
			//新的分值
			$newperformance = $oldperformance+$score;
			
			$pre['performance'] = $newperformance;
			$info = $users->where($arr1)->save($pre);
			if($info){
				echo "撤销成功";
			}else{
				echo "撤销失败";
			}
		}

		//班级绩效
		public function classPun(){
			$class = D("class");
			// $list = $class->select();

			//是否按照班级查找
			if(!empty($_GET['class'])){
				$tot = $class->where(array("id"=>$_GET['class']))->count();
			}else{
				$tot = $class->count();
			}

			//分页管理
			$nums = 5;
	    	$page = new \Think\Page($tot,5);
			$users = D("users");
			$class = D("class");

			//班级列表
			$classs = $class->select();
			$class_room = D("class_room");

			$search = "";
			//是否按照班级查找
			if(!empty($_GET['class'])){
				$list = $class->where(array("id"=>$_GET['class']))->limit($page->firstRow,$page->listRows)->select();
				$search="/id/{$_GET['class']}";
			}else{
				$list = $class->limit($page->firstRow,$page->listRows)->select();
			}

			foreach($list as $key=>$value){
				//班主任
				$arr1 = array("id"=>$value['admin_id']);
				$info1 = $users->where($arr1)->find();
				$list[$key]['admin_id'] = $info1['name'];

				//讲师
				$arr2 = array("id"=>$value['teacher_id']);
				$info2 = $users->where($arr2)->find();
				$list[$key]['teacher_id'] = $info2['name'];

				//项目经理id
				$arr3 = array("id"=>$value['pm_id']);
				$info3 = $users->where($arr3)->find();
				$list[$key]['pm_id'] = $info3['name'];
				//班级状态
				switch($value['status']){
					case 0:
						$list[$key]['status'] = "即将开班";
						break;
					case 1:
						$list[$key]['status'] = "正在上课";
						break;
					case 2:
						$list[$key]['status'] = "已毕业";
						break;
				}

				//教室
				$arr4 = array("id"=>$value['class_room']);
				$info4 = $class_room->where($arr4)->find();
				$list[$key]['class_room'] = $info4['name'];
			}

			// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);
			$this->assign("classs",$classs);
			$this->assign("list",$list);
			$this->assign("search",$search);
			$this->display("Punish/classPun");
		}

		//导出指定班级的Excel表
		public function check(){
			//班级id
			$arr1 = array("class"=>$_GET['id']);
			//生效的奖惩
			$arrs = array("status"=>1);
			$performance = D("performance");

			$list = $performance->where($arr1)->where($arrs)->limit($page->firstRow,$page->listRows)->select();

			$class = D("class");
			$users = D("users");
			foreach ($list as $key => $value) {
				//班级
				$arr2 = array("id"=>$value['class']);
				$info2 = $class->where($arr2)->find();
				$list[$key]['class'] = rtrim($info2['name'],"班")."班";

				//学员姓名
				$arr3 = array("id"=>$value['stu_id']);
				$info3 = $users->where($arr3)->find();
				$list[$key]['stu_id'] = $info3['name'];
				//惩罚执行人
				$arr4 = array("id"=>$value['do_id']);
				$info4 = $users->where($arr4)->find();
				$list[$key]['do_id'] = $info4['name'];
				//扣的分数
				$list[$key]['score'] = $list[$key]['score']."分";
				//执行时间
				$list[$key]['created'] = date("Y-m-d H:i:s",$list[$key]['created']);
			}
			$classname = rtrim($class->find($_GET['id'])["name"], "班")."班绩效数据";
			PHPExcel(3, $classname, $list);
		}



	}
?>